<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * 栏目管理控制器
 */
 class CategoryController extends  IsLoginController{
 	
	//定义栏目扩张模型
 	public $Model;
	public function __construct(){
        parent::__construct();
        $this->Model = D("category");
	}
		 	
 	/**
	 * 栏目管理列表首页
	 * 
	 */
	 public function index(){
	 	//查询栏目列表
	 	$cateList = M('category')->select();
		//实例化扩展类 栏目树
		$cate = new \Org\Util\Data;
		$cateList = $cate::tree($cateList,'name','cid','pid');
		//分配视图
		$this->assign('cateList',$cateList);
	 	$this->display();
	 }
	 
	 /**
	  * 添加栏目
	  */
	  public function addCate(){
	  	if(IS_POST){
	  		if(!$this->Model->addCategory()){
	  			$this->error($this->Model->getError());
	  		}
			$this->success('栏目添加成功',U('category/index'));
	  	}
		//查询分配模型信息
		$ModelM = M("model")->select(); 
		//查询栏目信息
		$cate = M("category")->select();
		
		
		//分配视图
		$this->assign('ModelM',$ModelM);
		$this->assign('cate',$cate);
	  	$this->display();
	  }
	  
	  	 /**
	  * 添加栏目
	  */
	  public function editCate(){
	  	$cid = I('get.cid');
	  	if(IS_POST){
	  		if(!$this->Model->editCategory()){
	  			$this->error($this->Model->getError());
	  		}
			$this->success('栏目修改成功',U('category/index'));
	  	}
		//查询分配模型信息
		$ModelM = M("model")->select(); 
		//查询栏目信息
		$cate = M("category")->select();
		
		
		//分配视图
		$this->assign('ModelM',$ModelM);
		$this->assign('cate',$cate);
		//查询当前栏目旧数据分配试图
		$oldcate = M("category")->where("cid='{$cid}'")->find();
		//查询栏目模型
		$mid = $oldcate['duosute_model_mid'];
		$modelName = current(M('model')->field('mname')->where("mid ='{$mid}'")->find());
		$this->assign('oldcate',$oldcate);
		$this->assign('modelName',$modelName);
	  	$this->display();
	  }
	
	/**
	 * 删除栏目
	 */
	 public function delCate(){
		if(IS_GET){
	  		if(!$this->Model->delCategory()){
	  			$this->error($this->Model->getError());
	  		}
			$this->success('栏目删除成功',U('category/index'));
	  	}
	 }
	 
	
	
	/*
	 * 栏目->文章列表
	 */
	 public function articleList(){
	 	//获取栏目id 将模型字段 分配试图调用对应发布方法
	 	$cid = I('get.cid','0','int');
		//通过cid查询模型id
		$moduleId = M('category')->where("cid = '{$cid}'")->field('duosute_model_mid')->find(); 
		$mid = $moduleId['duosute_model_mid'];
		$modelField = M('model')->where("mid = '{$mid}'")->find();
		//查询当前栏目文章内容
		$defalutModel = $modelField['mdefault_field'];//默认模型字段名
		$defalutModel = $modelField['mmodel_name'];//独立模型字段名
		//关联表 取出cid下所有文章
//		$join = $defalutModel .'join'. $defalutModel  .'on' . $defalutModel . '.aid='.$defalutModel .'.aid';
//		$artList = M()->JOIN($join)->where("cid='{$cid}'")->select();p($artList);
		

		
		$ariticleCount = M("defaultonmodel")->where("cid='{$cid}'")->count();	
		$Page = new \Think\Page($ariticleCount,C('PAGE_SIZE'));// 实例化分页类 传入总记录数和每页显示的记录数(15)
		$Page->setConfig('header','<div class="message">共<i class="blue">%TOTAL_ROW%</i>条记录，当前显示第&nbsp;<i class="blue">%NOW_PAGE%&nbsp;</i>页，共&nbsp;<i class="blue">%TOTAL_PAGE%&nbsp;</i>页</div>');
	    $Page->setConfig('prev','');
	    $Page->setConfig('next','');
	    $Page->setConfig('last','末页');
	    $Page->setConfig('first','首页');
	    $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
		$show = $Page->show();// 分页显示输出
		$artList = M('defaultonmodel')->where("cid='{$cid}'")->limit($p,C('PAGE_SIZE'))->select();
		$this->assign('artList',$artList)->assign('page',$show);
		$this->assign('modelField',$modelField);
		$this->assign('cid',$cid);
	 	$this->display();
	 }
	 
	 /**
	  * 获取栏目列表
	  */
	  public function cateList(){
	  	$cid = I('post.id',0,'intval');
		$cateList = M('category')->where("pid ='{$cid}'")->field('cid,name,is_show,sort')->select();
		$this->ajaxReturn($cateList);
	  }
	  
	  
 }
